home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 2 code / Speed Development / Graph.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-06  |  4.1 KB  |  139 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Graph.c
  4. #
  5. #    Copyright © 1989-1990 Apple Computer, Inc.
  6. #    All rights reserved.
  7. #
  8. #    This file contains the library routines to support a simple graphing
  9. #    algorithm.
  10. #
  11. ------------------------------------------------------------------------------*/
  12.  
  13. /* The list of includes will be limited to those that are available with every
  14.    C compiler. So, even though this engine is written in MPW, we'll limit the list
  15.    of includes to just those files that are common across development environments */
  16.    
  17.    
  18. #include <StdDef.h>                /* common definitions */
  19. #include <StdLib.h>                /* General utilities */
  20. #include <Graph.h>                /* our graphics engine stuff */
  21.  
  22. GraphStructPtr DoGraphInit( GraphType whichGraphType )
  23. {
  24.     GraphStructPtr    graphStorage = 0;
  25.     short            counter;
  26.     GraphValue        aGraphValue;
  27.     
  28.     switch ( whichGraphType ) {
  29.         case kBar:
  30.             if (!(graphStorage = (GraphStructPtr) malloc( sizeof (GraphStruct))))
  31.                 return 0;                /* error... */
  32.             graphStorage->numPoints = graphStorage->top = graphStorage->left = 
  33.             graphStorage->bottom = graphStorage->right =
  34.             graphStorage->graphYMax = graphStorage->graphYMin = 0;
  35.             for (counter = 0; counter < kMaxPoints; counter++ ) {
  36.                 aGraphValue = graphStorage->graphItems[counter];
  37.                 aGraphValue.whichOne = aGraphValue.value =
  38.                 aGraphValue.top = aGraphValue.left = 
  39.                 aGraphValue.right = aGraphValue.bottom = 0;
  40.             }
  41.             break;
  42.         case kStackedBar:
  43.         case kPie:
  44.         case kLine:
  45.             /* these are unsupported in this version */
  46.             break;
  47.     }
  48.     return graphStorage;
  49. }
  50.  
  51. void DoGraphSetGraphRect( short top, short left, short bottom, short right,
  52.                             GraphStructPtr graphStorage )
  53. {
  54.     graphStorage->top = top;
  55.     graphStorage->left = left;
  56.     graphStorage->bottom = bottom;
  57.     graphStorage->right = right;
  58. }
  59.  
  60. void DoGraphSetPoint( short which, long value, GraphStructPtr graphStorage )
  61. {
  62.     if ((which < 1) || (which > kMaxPoints))
  63.         return;                                    /* can't set this point - out of range */
  64.         
  65.     if (which > graphStorage->numPoints)
  66.         graphStorage->numPoints = which;        /* assign a new number of points */
  67.     
  68.     if (value > graphStorage->graphYMax)
  69.         graphStorage->graphYMax = value;        /* assign a new maximum */
  70.     
  71.     if (value < graphStorage->graphYMin)
  72.         graphStorage->graphYMin = value;        /* assign a new minimum */
  73.     
  74.     graphStorage->graphItems[which - 1].whichOne = which;
  75.     graphStorage->graphItems[which - 1].value = value;
  76. }
  77.  
  78. short DoGraphGetNumPoints( GraphStructPtr graphStorage )
  79. {
  80.     return graphStorage->numPoints;
  81. }
  82.  
  83. void DoGraphComputeBars( GraphStructPtr    graphStorage )
  84. /* NOTE assumes the following graphics coordinates: 0,0 at topleft */
  85. {
  86.     short            i,graphWidth,graphHeight,yRange;
  87.     GraphValue        aGraphValue;
  88.     
  89.     graphWidth = (graphStorage->right - graphStorage->left)
  90.                         / graphStorage->numPoints;
  91.     graphHeight = graphStorage->bottom - graphStorage->top;
  92.     yRange = graphStorage->graphYMax - graphStorage->graphYMin;
  93.     for (i = 0;  i < graphStorage->numPoints; i++) {
  94.         aGraphValue = graphStorage->graphItems[i];
  95.         aGraphValue.left = graphStorage->left + (i * graphWidth);
  96.         aGraphValue.right = aGraphValue.left + graphWidth;
  97.         aGraphValue.bottom = graphStorage->bottom;
  98.         aGraphValue.top = graphStorage->bottom -
  99.                 (graphHeight * (aGraphValue.value - graphStorage->graphYMin) / yRange);
  100.         aGraphValue.left += kMargin;
  101.         graphStorage->graphItems[i] = aGraphValue;
  102.     }
  103. }
  104.  
  105. short DoGraphGetYMax( GraphStructPtr graphStorage )
  106. {
  107.     return graphStorage->graphYMax;
  108. }
  109.  
  110. short DoGraphGetYMin( GraphStructPtr graphStorage )
  111. {
  112.     return graphStorage->graphYMin;
  113. }
  114.  
  115. short DoGraphGetBar( short which, short *top, short *left, short *bottom, short *right,
  116.                     GraphStructPtr graphStorage )
  117. /* DoGraphGetBar assigns the dimensions of the bar for this point in the bar chart and also
  118.    returns the value of this bar */
  119. {
  120.     GraphValue aGraphValue;
  121.  
  122.     aGraphValue = graphStorage->graphItems[which-1];
  123.     if (aGraphValue.whichOne != which)
  124.         return 0;
  125.     else {
  126.         *top = aGraphValue.top;
  127.         *left = aGraphValue.left;
  128.         *bottom = aGraphValue.bottom;
  129.         *right = aGraphValue.right;
  130.         return aGraphValue.value;
  131.     }
  132. }
  133.  
  134. GraphStructPtr DoGraphDispose( GraphStructPtr graphStorage )
  135. {
  136.     free(graphStorage);
  137.     return NULL;
  138. }
  139.